
import { reactive } from "vue";

interface loginUser {
  email: string;
  password: string;
  role: string;
  code_1: string;
  code_2: string;
}


export const loginUser = reactive<loginUser>({
  email: "a1@a.com",
  password: "123456",
  role: "user",
  code_1: "",
  code_2:""
});


interface loginRules {
  
  email: {
    type: string;
    message: string;
    required: boolean;
    trigger: string;
  }[];
  password: ({
    message: string;
    required: boolean;
    trigger: string;
  } | {
    min: number;
    max: number;
    message: string;
    trigger: string;

  })[];
  type: {
    type: string;
    required: boolean;
    message: string;
    trigger: string;
  }[];
  code_1: ({
    required: boolean;
    message: string;
    trigger: string;
    validator?: undefined;
} | {
    validator: (rule: loginRules, value: string, callback: any) => void;
    trigger: string;
    required?: undefined;
    message?: undefined;
})[];
}

const validatePass_2 = (rule:loginRules,value:string ,callback:any) => {
  if (value === "") {
    callback(new Error("验证码错误！"));
  } else if (value !== loginUser.code_2) {
    callback(new Error("验证码错误！"));
  } else {
    callback();
  }
};

// 校验规则
export const loginRules = reactive({

  email: [
    {
      type: "email",
      message: "请正确输入邮箱！",
      required: true,
      trigger: "blur",
    },
  ],
  password: [
    { message: "请输入密码！", required: true, trigger: "blur" },
    {
      min: 6,
      max: 20,
      message: "密码长度6-20位",
      trigger: "blur",
    },
  ],
  type: [
    {
      type: 'array',
      required: true,
      message: 'Please select at least one activity type',
      trigger: 'change',
    },
  ],
  code_1: [
    {
      required: true,
      message: "请输入验证码",
      trigger: "blur",
    },
    { validator: validatePass_2, trigger: "blur" },
  ],
});

